package ${package.Controller};

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ${package.Service}.${table.serviceName};
import ${package.Parent}.dao.${entity};
import ${package.Parent}.vo.Result;
import ${package.Parent}.vo.StatusCode;
import ${package.Parent}.group.AddGroup;
import ${package.Parent}.group.UpdateGroup;
import org.springframework.validation.annotation.Validated;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.CollectionUtils;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import java.net.URLEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
<#if swagger>
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.*;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * ${table.comment!} 前端控制器
 *
 * @author ${author}
 * @since ${date}
 */
<#if swagger>
@Tag(name = "${table.comment!}接口文档")
@ApiResponses({
        @ApiResponse(responseCode = "200", ref = "resultResponses"),
        @ApiResponse(responseCode = "400", ref = "responses400"),
        @ApiResponse(responseCode = "401", ref = "responses401"),
        @ApiResponse(responseCode = "403", ref = "responses403"),
        @ApiResponse(responseCode = "404", ref = "responses404"),
        @ApiResponse(responseCode = "500", ref = "responses500"),
        @ApiResponse(responseCode = "503", ref = "responses503")
})
</#if>
@Slf4j
@SaCheckLogin
@RestController
@RequestMapping("/<#if controllerMappingHyphenStyle??>${entity?uncap_first}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${entity?uncap_first}Service;


    /***
     * 条件查询${table.comment!}数据并导出Excel
     * @param ${entity?uncap_first} 查询条件实体
     * @return 导出结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    @Operation(summary = "条件查询${table.comment!}数据并导出Excel",description = "条件查询${table.comment!}数据并导出Excel", hidden = false)
    @PostMapping(value = "/export/search", produces = {"application/xml", "application/json"})
    public void exportSearch(@RequestBody(required = false) <#if swagger>@Parameter(name = "${entity}对象", required = true)</#if> ${entity} ${entity?uncap_first}, HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("${table.comment!}数据", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
            List<${entity}> list = ${entity?uncap_first}Service.list(new QueryWrapper<${entity}>(${entity?uncap_first}));
            EasyExcel.write(response.getOutputStream(),${entity}.class).sheet("${table.comment!}数据").doWrite(list);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }


    /***
     * 从Excel导入${table.comment!}数据
     * @param file Excel文件
     * @return 导入结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    @Operation(summary = "从Excel导入${table.comment!}数据",description = "从Excel导入${table.comment!}数据", hidden = false)
    @PostMapping(value = "/import")
    public Result importExcel(@RequestPart("file") MultipartFile file) throws IOException {
    List<${entity}> ${entity?uncap_first}List = EasyExcel.read(file.getInputStream())
        .head(${entity}.class)
        .sheet()
        .doReadSync();
        if (CollectionUtils.isEmpty(${entity?uncap_first}List)){
            return Result.OK("无法识别导入内容");
        }else {
            ${entity?uncap_first}Service.saveBatch(${entity?uncap_first}List);
            return Result.OK("导入成功");
        }
    }


    /***
     * 条件分页查询${table.comment!}数据
     * @param ${entity?uncap_first} 查询条件实体
     * @param page 当前页数
     * @param size 每页大小
     * @return 查询结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    <#if swagger>
    @Operation(summary = "条件分页查询${table.comment!}数据", description = "条件分页查询${table.comment!}数据", hidden = false)
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    </#if>
    @PostMapping(value = "/search/{page}/{size}", produces = {"application/xml", "application/json"}, consumes = {"application/json", "application/xml"})
    public Result findPage(@RequestBody(required = false) <#if swagger>@Parameter(name = "${entity}对象", required = true)</#if> ${entity} ${entity?uncap_first}, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size) {
        return ${entity?uncap_first}Service.findPage(${entity?uncap_first}, page, size);
    }


    /***
     * 分页查询所有${table.comment!}数据
     * @param page 当前页数
     * @param size 每页大小
     * @return 查询结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    <#if swagger>
    @Operation(summary = "分页查询${table.comment!}数据", description = "分页查询${table.comment!}数据", hidden = false)
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    </#if>
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@PathVariable(value = "page") int page, @PathVariable(value = "size") int size) {
         return ${entity?uncap_first}Service.findPage(page, size);
    }


    /***
     * 条件查询${table.comment!}数据
     * @param ${entity?uncap_first} 查询条件实体
     * @return 查询结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    <#if swagger>
    @Operation(summary = "条件查询${table.comment!}数据", description = "条件查询${table.comment!}数据", hidden = false)
    </#if>
    @PostMapping(value = "/search", produces = {"application/xml", "application/json"}, consumes = {"application/json", "application/xml"})
    public Result findList(@RequestBody(required = false) <#if swagger>@Parameter(name = "${entity}对象", required = true)</#if> ${entity} ${entity?uncap_first}) {
        return ${entity?uncap_first}Service.findList(${entity?uncap_first});
    }


    /***
     * 根据Id删除${table.comment!}数据
     * @param id 删除的数据Id
     * @return 删除结果
     */
    @SaCheckPermission("${entity?uncap_first}_delete")
    <#if swagger>
    @Operation(summary = "根据Id删除${table.comment!}数据", description = "根据Id删除${table.comment!}数据", hidden = false)
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    </#if>
    @Transactional
    @DeleteMapping(value = "/{id}", produces = {"application/xml", "application/json"})
    public Result delete(@PathVariable(value = "id") Integer id) {
        return ${entity?uncap_first}Service.deleteById(id);
    }


    /***
     * 根据Id批量删除${table.comment!}数据
     * @param ids 删除的数据Id集合
     * @return 删除结果
     */
    @SaCheckPermission("${entity?uncap_first}_delete")
    <#if swagger==true>
    @Operation(summary = "根据Id批量删除${table.comment!}数据", description = "根据Id批量删除${table.comment!}数据", hidden = false)
    </#if>
    @Transactional
    @DeleteMapping(produces = {"application/xml", "application/json"})
    public Result deleteByIds(@RequestBody <#if swagger>@Parameter(name = "需要删除的${entity}对象id集合", required = true)</#if> List<Integer> ids) {
        return ${entity?uncap_first}Service.deleteByIds(ids);
    }


    /***
     * 根据Id修改${table.comment!}数据
     * @param ${entity?uncap_first} 修改数据实体
     * @return 修改结果
     */
    @SaCheckPermission("${entity?uncap_first}_update")
    <#if swagger==true>
    @Operation(summary = "根据Id修改${table.comment!}数据", description = "根据Id修改${table.comment!}数据", hidden = false)
    </#if>
    @Transactional
    @PutMapping(produces = {"application/xml", "application/json"}, consumes = {"application/json", "application/xml"})
    public Result updateById(@RequestBody <#if swagger>@Parameter(name = "${entity}对象", required = true)</#if> @Validated(UpdateGroup.class) ${entity} ${entity?uncap_first}) {
        return ${entity?uncap_first}Service.update(${entity?uncap_first});
    }


    /***
     * 新增${table.comment!}数据
     * @param ${entity?uncap_first} 新增数据实体
     * @return 新增结果
     */
    @SaCheckPermission("${entity?uncap_first}_add")
    <#if swagger>
    @Operation(summary = "新增${table.comment!}数据", description = "新增${table.comment!}数据", hidden = false)
    </#if>
    @Transactional
    @PostMapping(produces = {"application/xml", "application/json"}, consumes = {"application/json", "application/xml"})
    public Result add(@RequestBody <#if swagger>@Parameter(name = "${entity}对象", required = true)</#if> @Validated(AddGroup.class) ${entity} ${entity?uncap_first}) {
        return ${entity?uncap_first}Service.add(${entity?uncap_first});
    }


    /***
     * 根据Id查询${table.comment!}数据
     * @param id 查询的数据Id
     * @return 查询结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    <#if swagger>
    @Operation(summary = "根据Id查询${table.comment!}数据", description = "根据Id查询${table.comment!}数据", hidden = false)
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    </#if>
    @GetMapping(value = "/{id}", produces = {"application/xml", "application/json"})
    public Result findById(@PathVariable(value = "id") Integer id) {
        return ${entity?uncap_first}Service.findById(id);
    }


    /***
     * 根据Id批量查询${table.comment!}数据
     * @param ids 查询的数据Id集合
     * @return 查询结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    <#if swagger>
    @Operation(summary = "根据Id批量查询${table.comment!}数据", description = "根据Id批量查询${table.comment!}数据", hidden = false)
    </#if>
    @PostMapping(value = "/ids", produces = {"application/xml", "application/json"})
    public Result findByIds(@RequestBody <#if swagger>@Parameter(name = "需要查询的${entity}对象id集合", required = true)</#if> List<Integer> ids) {
        return ${entity?uncap_first}Service.findByIds(ids);
    }


    /***
     * 查询所有${table.comment!}数据
     * @return 查询结果
     */
    @SaCheckPermission("${entity?uncap_first}_page")
    <#if swagger==true>
    @Operation(summary = "查询所有${table.comment!}数据", description = "查询所有${table.comment!}数据", hidden = false)
    </#if>
    @GetMapping(produces = {"application/xml", "application/json"})
    public Result findAll() {
        return ${entity?uncap_first}Service.findAll();
    }

}
</#if>
